/* eslint-disable array-callback-return */
/* eslint-disable react/destructuring-assignment */
/* eslint-disable no-nested-ternary */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-shadow */
// 字典下拉
import React from 'react';
import { Select, Radio, Checkbox } from 'antd';
import { connect } from 'dva';
import HOCSelect from '@/components/AntDesign/Select';

const { Option } = Select;
const RadioGroup = Radio.Group;
const CheckboxGroup = Checkbox.Group;

@connect(({ publicdata: { dictCode } }) => ({
  dictCode,
}))
class DictCode extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      value: props.value || [],
    };
  }

  componentDidMount() {
    const { dispatch, dictType } = this.props;
    dispatch({
      type: 'publicdata/fetchDictCode',
      payload: [dictType] || [],
    });
  }

  handleChange = value => {
    if (!('value' in this.props)) {
      this.setState({ value });
    }
    this.triggerChange(value);
  };

  triggerChange = changedValue => {
    // Should provide an event to pass value to Form.
    const { onChange } = this.props;
    if (onChange) {
      onChange(changedValue);
    }
  };

  render() {
    const { inputType = '1', dictCode = {}, dictType = [] } = this.props;
    // 复选框，单选内容展示
    const options = dictType => {
      const arr = [];
      dictCode[dictType] &&
        dictCode[dictType].map(items => {
          const obj = {};
          obj.label = items.itemName;
          obj.value = items.itemCode;
          arr.push(obj);
        });
      return arr;
    };
    return inputType === '1' ? ( // 默认1：下拉框:2：复选框，3：单选按钮
      <HOCSelect
        placeholder="请选择"
        {...this.props}
        value={this.props.value}
        showSearch
        optionFilterProp="children"
      >
        {dictCode[dictType] &&
          dictCode[dictType].map(item => (
            <Option key={item.itemCode} fsOptions={item}>
              {item.itemName}
            </Option>
          ))}
      </HOCSelect>
    ) : inputType === '2' ? (
      <CheckboxGroup
        options={options(dictType)}
        value={this.props.value || this.state.value}
        onChange={this.handleChange}
        disabled={this.props.disabled || false}
      />
    ) : (
      <RadioGroup options={options(dictType)} {...this.props} />
    );
  }
}
export default DictCode;
